package bigdata.task;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import datacenterbizapiexternal.datapushnew.DataPushNewClient;
import datacenterbizapiexternal.datapushnew.service.DataPushNewService;
import framework.util.CollectionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import datacenterbizapiexternal.datapush.DataPushClient;
import datacenterbizapiexternal.datapush.domain.DataPushExternalEnum;
import datacenterbizapiexternal.datapush.domain.entity.DataPushDistribution;
import datacenterbizapiexternal.datapush.domain.entity.DataPushEntity;
import datacenterbizapiexternal.datapush.domain.entity.RealTimeHeatMap;
import datacenterbizapiexternal.datapush.domain.entity.RealTimeHeatMapEntity;
import datacenterbizapiexternal.datapush.domain.entity.TourTraceDistribution;
import datacenterbizapiexternal.datapush.domain.entity.TourTraceDistributionEntity;
import datacenterbizapiexternal.datapush.service.DataPushDistributionService;
import datacenterbizapiexternal.datapush.service.RealTimeHeatMapService;
import datacenterbizapiexternal.datapush.service.TourTraceDistributionService;
/*import datacenterbizapiexternal.infrared.InfraRedClient;
import datacenterbizapiexternal.infrared.domain.InfraRedIPS;
import datacenterbizapiexternal.infrared.domain.entity.InfrRedHistoryDataVerification;
import datacenterbizapiexternal.infrared.domain.entity.InfraRedHistoryData;
import datacenterbizapiexternal.infrared.domain.responseparams.InfraRedResponse;
import datacenterbizapiexternal.infrared.service.InfraRedService;
import datacenterbizapiexternal.videopassenger.VideoPassengerClient;
import datacenterbizapiexternal.videopassenger.domain.entity.VideoPassengerTimeSum;
import datacenterbizapiexternal.videopassenger.domain.entity.VideoPassengerTimeValue;
import datacenterbizapiexternal.videopassenger.domain.responseParams.VideoPassengerResponseParams;
import datacenterbizapiexternal.videopassenger.service.VideoPassengerService;
import datacenterbizapiexternal.videopassenger.service.VideoPassengerTimeValueService;*/
import framework.util.date.DateStyle;
import framework.util.date.DateUtil;



@SuppressWarnings({ "unchecked", "rawtypes" })
public class DataPushTask {
	private Logger logger = LoggerFactory.getLogger(DataPushTask.class);
	@Autowired
	private DataPushDistributionService dataPushDistributionService;
	@Autowired
	private RealTimeHeatMapService realTimeHeatMapService;
	@Autowired
	private TourTraceDistributionService tourTraceDistributionService;

	@Autowired
	private DataPushNewService dataPushNewService;
	/*@Autowired
	private VideoPassengerTimeValueService videoPassengerTimeValueService;
	@Autowired
	private InfraRedService infraRedService;
	@Autowired
	private VideoPassengerService videoPassengerService;*/
	static boolean flg = false, flgs = false;
	static int count = 0;

	public List<DataPushEntity<DataPushDistribution>> listAddDataPushEnity(
			String time, DataPushExternalEnum dataPushEnum,
			List<DataPushEntity<DataPushDistribution>> list) {
		DataPushEntity enity = dataPushDistributionService
				.queryEntityByTimeAndType(time, dataPushEnum.getType());
		if (enity == null) {
			DataPushEntity<DataPushDistribution> distribution = DataPushClient
					.getDataPushObj(time, dataPushEnum);
			if (distribution != null)
				list.add(distribution);
		}
		return list;
	}

	public List<DataPushEntity<DataPushDistribution>> batchListAddDataPushEnity(
			String time, List<DataPushEntity<DataPushDistribution>> list) {

		list = listAddDataPushEnity(time, DataPushExternalEnum.TOURIST_TOTAL,
				list);
		list = listAddDataPushEnity(time,
				DataPushExternalEnum.AGE_DISTRIBUTION, list);
		list = listAddDataPushEnity(time,
				DataPushExternalEnum.GENDER_DISTRIBUTION, list);
		list = listAddDataPushEnity(time,
				DataPushExternalEnum.CONSUME_DISTRIBUTION, list);
		list = listAddDataPushEnity(time,
				DataPushExternalEnum.ORIGIN_PROVINCE_DISTRIBUTION, list);
		list = listAddDataPushEnity(time,
				DataPushExternalEnum.ORIGIN_CITY_DISTRIBUTION, list);
		list = listAddDataPushEnity(time,
				DataPushExternalEnum.DURATION_ESTIMATE, list);
		return list;
	}

	/**
	 * @Title: dataPushDistribution
	 * @Description: 个推各种分布数据获取
	 * @param
	 * @return void 返回类型
	 */
	public void dataPushDistributionTask() {
		// logger.info("执行DataPushTask.dataPushDistributionTask开始");
		/**
		 * 根据个推提供 数据获取最晚上个月
		 */
		String time = DateUtil.DateToString(DateUtil.addMonth(new Date(), -1),
				"yyyyMM");
		dataPushDistributionTask(time);
		// logger.info("执行DataPushTask.dataPushDistributionTask结束");
	}

	/**
	 * @Title: dataPushDistributionTask2
	 * @Description: 每天游客总量
	 * @param
	 * @return void 返回类型
	 */
	public void dataPushDistributionTask2() {
		// logger.info("执行DataPushTask.dataPushDistributionTask开始");
		/**
		 * 根据个推提供 数据获取最晚上个月
		 */
		String time = DateUtil.DateToString(DateUtil.addMonth(new Date(), -1),
				"yyyyMM");
		dataPushDistributionTask(time);
		// logger.info("执行DataPushTask.dataPushDistributionTask结束");
	}

	public void dataPushDistributionTask(String time) {
		List<DataPushEntity<DataPushDistribution>> list = new ArrayList<DataPushEntity<DataPushDistribution>>();

		list = batchListAddDataPushEnity(time, list);

		if (list.size() > 0) {
			dataPushDistributionService.addDataPushEntities(list);
			String distributions = "";
			for (DataPushEntity<DataPushDistribution> dataPushEntity : list) {
				distributions += dataPushEntity.getDescription() + ",";
			}
			logger.info("数据时间为" + time + "的相关" + distributions
					+ "分布数据,更新到本地数据库");
		} else {
			logger.warn("执行DataPushTask.dataPushDistributionTask结束,未添加个推数据到数据库");
		}
	}

	/**
	 * 
	 * @Title: tourTraceDistributionTask
	 * @Description: 游览轨迹数据获取
	 * @param
	 * @return void 返回类型
	 */
	public void tourTraceDistributionTask() {
		// logger.info("执行DataPushTask.tourTraceDistributionTask开始");
		String time = DateUtil.DateToString(DateUtil.addMonth(new Date(), -1),
				"yyyyMM");
		TourTraceDistributionEntity<TourTraceDistribution> tourTraceDistribution = DataPushClient
				.getTourTraceDistribution(time);
		DataPushEntity enity = dataPushDistributionService
				.queryEntityByTimeAndType(time,
						DataPushExternalEnum.TOUR_TRACE_DISTRIBUTION.getType());
		int totle = getTotle(time);
		if (enity == null) {
			tourTraceDistribution.setTotal(totle);
			tourTraceDistributionService.addEntity(tourTraceDistribution);
			logger.info("数据时间为" + time + "的游览轨迹数据,更新到本地数据库");
		}
		logger.info("执行DataPushTask.tourTraceDistributionTask结束");
	}

	// 获取总人数
	public int getTotle(String time) {
		DataPushEntity<DataPushDistribution> distribution = DataPushClient
				.getDataPushObj(time, DataPushExternalEnum.TOURIST_TOTAL);
		if (null != distribution && distribution.getList() != null
				&& distribution.getList().size() > 0) {
			String value = distribution.getList().get(0).getValue();
			return Integer.parseInt(value);
		}
		return 0;

	}

	/**
	 * @Title: realTimeHeatTask
	 * @Description: 实时热力图数据
	 * @param
	 * @return void 返回类型
	 */
	public void realTimeHeatTask() {
		// logger.info("执行DataPushTask.realTimeHeatTask开始");
		String time = DateUtil.getRealTimeHeatMapHttpStart(new Date());
		RealTimeHeatMapEntity entity = realTimeHeatMapService
				.queryEntityByStartTimeThroughDB(new Date());
		if (entity == null) {
			/*RealTimeHeatMapEntity<RealTimeHeatMap> realTimeHeatMap = DataPushClient
					.getRealTimeHeatMap("");*/

			/*RealTimeHeatMapEntity realTimeHeatMap = realTimeHeatMapService
					.queryEntityByStartTimeThroughDB(new Date());*/

			DataPushNewClient.getRealTimeData(new Date());
			RealTimeHeatMapEntity realTimeHeatMap  = dataPushNewService.getRealTimeHeatMapEntity(new Date());
			//RealTimeHeatMapEntity realTimeHeatMap  = DataPushNewClient.getRealTimeData(new Date());
			if(realTimeHeatMap!=null)
				realTimeHeatMapService.addEntity(realTimeHeatMap);
			logger.info("数据时间为" + time + "的实时热力图数据,更新到本地数据库");
		}
		// logger.info("执行DataPushTask.realTimeHeatTask结束");
	}

	/**
	 * @Title: dailyTouristTotal
	 * @Description: 次日凌晨同步昨日日游客总量
	 * @param
	 * @return void 返回类型
	 */
	public void dailyTouristTotal() {
		Date date = DateUtil.addMonth(new Date(), -1);
		dailyTouristTotal(date);
	}

	public void dailyTouristTotal(Date date) {
		String time = DateUtil.DateToString(date,DateStyle.YYYYMM);
		DataPushEntity enity = dataPushDistributionService
				.queryEntityByTimeAndType(time,
						DataPushExternalEnum.TOURIST_TOTAL.getType());
		if (enity == null) {
			/*DataPushEntity<DataPushDistribution> distribution = DataPushClient
					.getDataPushObj(time, DataPushExternalEnum.TOURIST_TOTAL);*/
			DataPushEntity<DataPushDistribution> distribution = dataPushNewService.getTotalByMonth(date);
			if (distribution != null) {
				dataPushDistributionService.addDataPushEntity(distribution);
			}
		}
	}

	/**来源省份*/
	public void getTourFromPrvnRatio(Date date) {
		String time = DateUtil.DateToString(date,DateStyle.YYYYMM);
		DataPushEntity entity = dataPushDistributionService
				.queryEntityByTimeAndType(time,
						DataPushExternalEnum.ORIGIN_PROVINCE_DISTRIBUTION.getType());
		if (entity == null) {
			DataPushEntity<DataPushDistribution> dataEntity = dataPushNewService.getTourFromPrvnRatio(date);
			dataPushDistributionService.addDataPushEntity(dataEntity);
		}

	}
	public void getTourFromPrvnRatio() {
		Date date = DateUtil.addMonth(new Date(), -1);
		getTourFromPrvnRatio(date);

	}

	/**来源城市*/
	public void getTourFromCityRatio(Date date) {
		String time = DateUtil.DateToString(date,DateStyle.YYYYMM);
		DataPushEntity entity = dataPushDistributionService
				.queryEntityByTimeAndType(time,
						DataPushExternalEnum.ORIGIN_CITY_DISTRIBUTION.getType());
		if (entity == null) {
			DataPushEntity<DataPushDistribution> dataEntity = dataPushNewService.getTourFromCityRatio(date);
			dataPushDistributionService.addDataPushEntity(dataEntity);
		}
	}
	public void getTourFromCityRatio() {
		Date date = DateUtil.addMonth(new Date(), -1);
		getTourFromCityRatio(date);
	}
	/**消费分布*/
	public void getConsume() {

	}
	/**年龄分布*/
	public void getTourAge(Date date) {
		String time = DateUtil.DateToString(date,DateStyle.YYYYMM);
		DataPushEntity entity = dataPushDistributionService
				.queryEntityByTimeAndType(time,
						DataPushExternalEnum.AGE_DISTRIBUTION.getType());
		if (entity == null) {
			DataPushEntity<DataPushDistribution> dataEntity = dataPushNewService.getTourAge(date);
			dataPushDistributionService.addDataPushEntity(dataEntity);
		}
	}

	public void getTourAge() {
		Date date = DateUtil.addMonth(new Date(), -1);
		getTourAge(date);
	}
	/**性别分布*/
	public void getTourGender(Date date) {
		String time = DateUtil.DateToString(date,DateStyle.YYYYMM);
		DataPushEntity entity = dataPushDistributionService
				.queryEntityByTimeAndType(time,
						DataPushExternalEnum.GENDER_DISTRIBUTION.getType());
		if (entity == null) {
			DataPushEntity<DataPushDistribution> dataEntity = dataPushNewService.getTourGender(date);
			dataPushDistributionService.addDataPushEntity(dataEntity);
		}
	}

	public void getTourGender() {
		Date date = DateUtil.addMonth(new Date(), -1);
		getTourAge(date);
	}
	/**消费分布*/
	public void getTourConsume(Date date) {
		String time = DateUtil.DateToString(date,DateStyle.YYYYMM);
		DataPushEntity entity = dataPushDistributionService
				.queryEntityByTimeAndType(time,
						DataPushExternalEnum.CONSUME_DISTRIBUTION.getType());
		if (entity == null) {
			DataPushEntity<DataPushDistribution> dataEntity = dataPushNewService.getTourConsume(date);
			dataPushDistributionService.addDataPushEntity(dataEntity);
		}
	}

	public void getTourConsume() {
		Date date = DateUtil.addMonth(new Date(), -1);
		getTourConsume(date);
	}

	/**游客游览轨迹分布*/
	public void getTourRoutesRatio(Date date){
		String time = DateUtil.DateToString(date,DateStyle.YYYYMM);
		List<TourTraceDistributionEntity<TourTraceDistribution>> list = tourTraceDistributionService.queryEntityByStartTimeAndEndTime(time, time);
		if (CollectionUtil.isEmpty(list)) {
			TourTraceDistributionEntity<TourTraceDistribution> dataEntity = dataPushNewService.getTourRoutesRatio(date);
			tourTraceDistributionService.addEntity(dataEntity);
		}
	}
	public void getTourRoutesRatio() {
		Date date = DateUtil.addMonth(new Date(), -1);
		getTourRoutesRatio(date);
	}
	/**省份、城市、年龄、性别处理*/
	public void dataPushNormal(){
		Date date = DateUtil.addMonth(new Date(), -1);
		getTourFromPrvnRatio(date);
		getTourFromCityRatio(date);
		getTourAge(date);
		getTourGender(date);
		dailyTouristTotal(date);
		getTourConsume(date);
		getTourRoutesRatio(date);
	}

}
